package com.waterworld.watch.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.BindWatchUserBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.PhotoUtils;

import java.util.List;

/**
 * 编写者：Created by SunnyTang
 * 时间：2018/12/6 11:23
 * 主要作用：宝贝与其他用户的绑定列表(适配器)
 */
public class WatchOtherUserAdapter extends RecyclerView.Adapter<WatchOtherUserAdapter.MyHolder> {

    private Context mContext;
    private List<BindWatchUserBean> mData;

    public WatchOtherUserAdapter(Context mContext, List<BindWatchUserBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View viewHolder = inflater.inflate(R.layout.item_other_user, parent, false);
        return new MyHolder(viewHolder);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder myHolder, int position) {
        if (mData.get(position).getHead() == null) {
            Glide.with(mContext).load(R.drawable.headimage_default).into(myHolder.otherUserAvatar);
        } else if (mData.get(position).getHead().startsWith("sys")) {
            Glide.with(mContext).load(PhotoUtils.getResource(mData.get(position).getHead().substring(0,13))).into(myHolder.otherUserAvatar);
        } else {
            Glide.with(mContext).load(UrlContants.FILE_URL + "resources/watch/" + mData.get(position).getHead()).into(myHolder.otherUserAvatar);
        }
        if (TextUtils.isEmpty(mData.get(position).getName())) {
            myHolder.otherUserName.setText(mData.get(position).getUserRelation());
        } else {
            myHolder.otherUserName.setText(mData.get(position).getName());
        }
        myHolder.otherUserRelation.setText(mData.get(position).getUserRelation());
        myHolder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view,position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ConstraintLayout parent;
        CircleImageView otherUserAvatar;
        TextView otherUserName;
        TextView otherUserRelation;

        MyHolder(@NonNull View itemView) {
            super(itemView);
            otherUserAvatar = itemView.findViewById(R.id.iv_other_user_avatar);
            otherUserName = itemView.findViewById(R.id.tv_other_user_name);
            otherUserRelation = itemView.findViewById(R.id.tv_other_user_relation);
            parent = itemView.findViewById(R.id.cl_parent);
        }
    }

    public interface onClickListener {
        void onClick(View view,int position);
    }

    public onClickListener listener;

    public void setOnClickListener(onClickListener listener) {
        this.listener = listener;
    }
}
